home *** CD-ROM | disk | FTP | other *** search
/ Symantec Visual Cafe for Java 2.5 / symantec-visual-cafe-2.5-database-dev-edition.iso / Visual Cafe Pro v1.0 / SOURCE.BIN / DbDataSource.java < prev    next >
Encoding:
Java Source  |  1997-06-19  |  10.5 KB  |  388 lines

  1. /*
  2.  * DbDataSource.java   1.0   12 Jan 1997
  3.  *
  4.  * Copyright (c) 1996 Krumel & Associates, Inc.  All Rights Reserved.
  5.  *
  6.  * This software is provided as is.  Krumel & Associates shall not be liable
  7.  * for any damages suffered by licensee as a result of using, modifying or
  8.  * distributing this software or its derivatives.
  9.  */
  10. package symantec.itools.db.awt;
  11.  
  12. import java.awt.Point;
  13. import java.awt.Image;
  14.  
  15. public class DbDataSource implements DataSource {
  16.     Matrix          rowCache = new Matrix();
  17.  
  18.     Grid            view;
  19.     DbDataStore     store;
  20.     DbDataUpdater   updater;
  21.     MetaTable       meta;
  22.     boolean         caching = false;
  23.     Data            currData;
  24.     int             currDataRow, currDataCol;
  25.  
  26.     public DbDataSource(Grid g, DbDataStore s, DbDataUpdater u, MetaTable m) {
  27.         view = g;
  28.         setupSource(s, u, m);
  29.     }
  30.  
  31.     public Grid getView() {
  32.         return view;
  33.     }
  34.  
  35.     public void setupSource(DbDataStore s, DbDataUpdater u, MetaTable m) {
  36.         store = s;
  37.         store.setDbDataSource(this);
  38.         updater = u;
  39.         meta = m;
  40.  
  41.         if (meta != null) {
  42.             meta.setDbDataSource(this);
  43.         }
  44.  
  45.         //cache if store doesn't
  46.         caching = !store.supportsCaching();
  47.     }
  48.  
  49.     public void setGrid(Grid v) {
  50.         view = v;
  51.     }
  52.  
  53.     public void fetchMode(boolean manual) {
  54.         store.fetchMode(manual);
  55.     }
  56.  
  57.     public void setDefaultData(Data defaultValue) {}
  58.  
  59.     public void setDefaultData() {}
  60.  
  61.     public boolean supportsMeta() {
  62.         return meta != null;
  63.     }
  64.  
  65.     public Matrix getCache() { return rowCache; }
  66.  
  67.     public int lastCachedRow() {
  68.         return rowCache.rows() - 1;
  69.     }
  70.  
  71.     public MetaTable getMetaTable() { return meta; }
  72.  
  73.     public void setupGrid(Grid v) throws TypeNotSupported {
  74.         if (meta == null) {
  75.             throw new TypeNotSupported("MetaTable not set");
  76.         }
  77.  
  78.         meta.setupGrid(view);
  79.     }
  80.  
  81.     public void commitData() throws TypeNotSupported {
  82.         if (currData != null && currData.changed()) {
  83.             setData(currDataRow, currDataCol-1, currData);
  84.             currData.commit();
  85.             currData = null;
  86.             currDataRow = -1;
  87.         }
  88.     }
  89.  
  90.     public void setCurrentRow(int row) throws TypeNotSupported {
  91.         store.setCurrentRow(row);
  92.     }
  93.  
  94.     public Data readData(int row, int col) throws DataNotAvailable {
  95.         col++;
  96.  
  97.         if (currDataRow == row && currDataCol == col) {
  98.             return currData;
  99.         }
  100.  
  101.         //if caching and it is cached then return it
  102.         if (caching && rowCache.contains(row, col)) {
  103.             return (Data)rowCache.elementAt(row, col);
  104.         }
  105.  
  106.         //if not then go get it and then return it
  107.         //cache if store isn't caching for us
  108.             Data d = store.getData(row, col);
  109.  
  110.             if (caching) {
  111.                 rowCache.addElement(row, col, d);
  112.                 markClean(row);
  113.             }
  114.  
  115.             return d;
  116.     }
  117.  
  118.     //call this method when going to be doing edits
  119.     //call commitData() when done with it (usually when lose focus)
  120.     public Data getData(Coordinate coords) throws DataNotAvailable {
  121.         return getData(coords.row, coords.col);
  122.     }
  123.  
  124.     //call this method when going to be doing edits
  125.     //call commitData() when done with it (usually when lose focus)
  126.     public Data getData(int row, int col) throws DataNotAvailable {
  127.         col++;  //we maintain state information in col = 0 so it is all 1 based
  128.  
  129.         if (currDataRow == row && currDataCol == col) {
  130.             return currData;
  131.         }
  132.         currData = readData(row, col-1);
  133.         currDataRow = row;
  134.         currDataCol = col;
  135.  
  136.         return currData;
  137.     }
  138.  
  139.     public void addResultSetRow(int row, Data data[]) {
  140.         //row should already be zero relative by DbDataStore
  141.         //store has obtained a new row of information so store in matrix
  142.         //and mark row as clean
  143.  
  144.         //remember - row state stored in col 0
  145.         for (int col=1; col<=data.length; col++) {
  146.             rowCache.updateElement(row, col, data[col-1]);
  147.         }
  148.  
  149.         markClean(row);
  150.     }
  151.  
  152.     //It is assumed that data has been verified already by data object, but...
  153.     public void setData(int row, int col, Data data) throws TypeNotSupported {
  154.         col++;  //we maintain state information in col = 0
  155.         //if cached then mark row for update and change data
  156.         if (caching) {
  157.             rowCache.updateElement(row, col, data);
  158.             markModified(row);
  159.             return;
  160.         }
  161.  
  162.         //not caching so tell source to update
  163.         store.update(row, col, data);
  164.     }
  165.  
  166.     public void setData(Coordinate coord, Data data) throws TypeNotSupported {
  167.         setData(coord.row, coord.col, data);
  168.     }
  169.  
  170.     public String getText(Coordinate coords) throws DataNotAvailable {
  171.         return getData(coords).toString();
  172.     }
  173.  
  174.     public void undeleteRow(int row) throws TypeNotSupported {
  175.         if (caching) {
  176.             //for now just assume it was previously changed
  177.             //need to add ability to remember previous state through XOR
  178.             markModified(row);
  179.         }
  180.  
  181.         updater.undeleteRow(row);
  182.     }
  183.  
  184.     public void deleteRow(int row) throws TypeNotSupported {
  185.         if (caching) {
  186.             markDeleted(row);
  187.         }
  188.  
  189.         updater.deleteRow(row);
  190.     }
  191.  
  192.     public void insertRow(int row) throws TypeNotSupported {
  193.         updater.insertRow(row);
  194.     }
  195.  
  196.     public int appendRow() throws TypeNotSupported {
  197.         return updater.appendRow();
  198.     }
  199.  
  200.     public boolean supports(Coordinate coords, int type) {
  201.         if (type == Data.STRING) return true;
  202.  
  203.         return false;
  204.     }
  205.  
  206.     public Image getImage(Coordinate coords) throws DataNotAvailable {
  207.         return getData(coords).toImage();
  208.     }
  209.  
  210.     public boolean handleEvent(java.awt.Event e) {
  211.         switch(e.id) {
  212.             case view.UNDO_CELL_EVENT:
  213.                 rollback();
  214.                 break;
  215.             case view.LOST_FOCUS:
  216.                 break;
  217.         }
  218.         return false;
  219.     }
  220.  
  221.     public void handleException(int row, int col, Exception ex) {
  222.         view.handleException(row, col, ex);
  223.     }
  224.  
  225.     public int rowState(int row) {
  226.         return store.rowState(row);
  227.     }
  228.  
  229.     public void clear() {
  230.         rowCache.removeAllElements();
  231.         currData = null;
  232.         currDataRow = -1;
  233.         store.clear();
  234.     }
  235.  
  236.     public void refresh() {
  237.         //clear cache, clear view, and re-issue query
  238.         rowCache.removeAllElements();
  239.         currData = null;
  240.         currDataRow = -1;
  241.         store.refresh();
  242.     }
  243.  
  244.     public void undoRow(int row) throws TypeNotSupported {
  245.         store.undoRow(row);
  246.     }
  247.  
  248.     public void save() throws TypeNotSupported {
  249.         //iterate cache and update all non-clean rows
  250.         updater.save();
  251.     }
  252.  
  253.     public boolean isDataEditable(int row, int col) {
  254.         if (meta != null) {
  255.             try {
  256.                 return meta.isDataEditable(row, col+1);
  257.             } catch(DataNotAvailable ex) {
  258.                 return false;
  259.             }
  260.         }
  261.  
  262.         return true;
  263.     }
  264.  
  265.     protected void markModified(int row) {
  266.         RowState    state;
  267.  
  268.         if (rowCache.contains(row, 0)) {
  269.             state = (RowState)rowCache.elementAt(row, 0);
  270.             state.markModified();
  271.         } else {
  272.             //it is really new, not modified
  273.             state = new RowState();
  274.             rowCache.addElement(row, 0, state);
  275.             state.markNew();
  276.         }
  277.     }
  278.  
  279.     protected void markNew(int row) {
  280.         RowState    state;
  281.  
  282.         if (rowCache.contains(row, 0)) {
  283.             state = (RowState)rowCache.elementAt(row, 0);
  284.         } else {
  285.             state = new RowState();
  286.             rowCache.addElement(row, 0, state);
  287.         }
  288.  
  289.         state.markNew();
  290.     }
  291.  
  292.     protected void markDeleted(int row) {
  293.         //if something is being deleted it should already be there so
  294.         //let's assume it is there.
  295.         RowState state = (RowState)rowCache.elementAt(row, 0);
  296.         state.markDeleted();
  297.     }
  298.  
  299.     protected void markClean(int row) {
  300.         RowState    state;
  301.  
  302.         if (rowCache.contains(row, 0)) {
  303.             state = (RowState)rowCache.elementAt(row, 0);
  304.         } else {
  305.             state = new RowState();
  306.             rowCache.addElement(row, 0, state);
  307.         }
  308.  
  309.         state.markClean();
  310.     }
  311.  
  312.     public int validDataRowRange(int top, int bottom) throws DataNotAvailable {
  313.         //subtract one to make 0 relative
  314.         return store.validDataRowRange(top, bottom)-1;
  315.     }
  316.  
  317.     public int rows() {
  318.         return store.rowsRetrieved();
  319.     }
  320.  
  321.     public int fetchAllRows() {
  322.         return store.fetchAllRows();
  323.     }
  324.  
  325.     //Here are some methods needed to support DefaultData
  326.     public int type(int row, int col) {
  327.         return Data.STRING;
  328.     }
  329.  
  330.     public void rollback(int row, int col) { rollback(); }
  331.  
  332.     public void rollbackCurrentData() {
  333.         rollback();
  334.     }
  335.  
  336.     public void rollback() {
  337.         currData = null;
  338.         currDataRow = -1;
  339.     }
  340.  
  341.     public void commit(int row, int col) {}
  342.     public boolean isMasked(int row, int col) {
  343.         return false;
  344.     }
  345.  
  346.     public String getMask(int row, int col) throws TypeNotSupported {
  347.         throw new TypeNotSupported("stubbed");
  348.     }
  349.  
  350.     public boolean supportsChoice(int row, int col) {
  351.         return false;
  352.     }
  353.  
  354.     public Data[] getChoices(int row, int col) throws TypeNotSupported {
  355.         throw new TypeNotSupported("stubbed");
  356.     }
  357.  
  358.     public void setText(int row, int col, String t) {}
  359.  
  360.     //pos is space where to be inserted (0 = first char)
  361.     public void insertChar(int row, int col, int pos, char c) {}
  362.  
  363.     public void setText(int row, int col, char c) {}
  364.  
  365.     public void appendChar(int row, int col, char c) {}
  366.  
  367.     public void clearText(int row, int col) {}
  368.  
  369.     public void deleteChar(int row, int col, int pos) {}
  370.  
  371.     public String subString(int row, int col, int spos, int epos) {
  372.         return "stubbed";
  373.     }
  374.  
  375.     public void setImage(int row, int col, Image i) {}
  376.  
  377.     public String toString(int row, int col) {
  378.         return "stubbed";
  379.     }
  380.  
  381.     public Image toImage(int row, int col) {
  382.         return null;
  383.     }
  384.  
  385.     public Object getSynchronizationObject() {
  386.         return store.getSynchronizationObject();
  387.     }
  388. }